﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Web;
using System.Text.RegularExpressions;
using System.Threading;

namespace liteapp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void DownloadThread()
        {
            button1.Invoke(new Action(() => { button1.Enabled = false; }));
            UriBuilder parser = new UriBuilder(textBox1.Text);
            String querystr = parser.Query;
            HttpUtility.UrlEncode(querystr);
            String bookid = HttpUtility.ParseQueryString(querystr).Get("id");
            String outdir = textBox2.Text;
            outdir = outdir + bookid + "\\";
            System.IO.Directory.CreateDirectory(outdir);
            WebClient wc = new WebClient();
            String pagesrc = wc.DownloadString("http://books.google.com/books?id=" + bookid + "&pg=1&jscmd=click3");
            MatchCollection matches = Regex.Matches(pagesrc, "\"pid\":\"(?<pid>.*?)\"", RegexOptions.ExplicitCapture);
            String pid;
            String src;
            MatchCollection matches2;
            progressBar1.Invoke(new Action(() => { progressBar1.Maximum = matches.Count - 1; }));
            for (int i = 0; i < matches.Count; i++)
            {
                pid = matches[i].Result("${pid}");
                pagesrc = wc.DownloadString("http://books.google.com/books?id=" + bookid + "&pg=" + pid + "&jscmd=click3");
                matches2 = Regex.Matches(pagesrc, "\"pid\":\"" + pid + "\",\"src\":\"(?<src>.*?)\"", RegexOptions.ExplicitCapture);
                if (matches2.Count == 1)
                {
                    src = matches2[0].Result("${src}");
                    src = src.Replace("\\u0026", "&");
                    wc.DownloadFile(src, outdir + String.Format("{0:0000}", i + 1) + ".png");
                }
                progressBar1.Invoke(new Action(() => { progressBar1.Value = i; }));
            }
            MessageBox.Show("Done.");
            progressBar1.Value = 0;
            button1.Invoke(new Action(() => { button1.Enabled = true; }));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new Thread(DownloadThread).Start();  
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox2.Text = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\";
            linkLabel1.Links[0].LinkData = "http://www.gbooksdownloader.com/";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog directoryDialog = new FolderBrowserDialog();
            directoryDialog.RootFolder = Environment.SpecialFolder.DesktopDirectory;
            if (directoryDialog.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = directoryDialog.SelectedPath + "\\";
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
        }

    }
}
